<?php
/**
 * Created by PhpStorm.
 * User: zhangchangshun
 * Date: 17-3-16
 * Time: 下午4:43
 */
class concerns extends API_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->_auto();
        $this->load->model('concerns_model', 'mconcerns');
        $this->load->model('topic_model', 'mtopic');
        $this->load->model('circle_model','mcircle');

    }
    //添加关注
    public function cadd() {
        $article_info = $this->mtopic->get_one(array('id'=>$this->data['tid']));
        if(empty($article_info)){
            $this->vdata['returnCode'] = '401';
            $this->vdata['returnInfo'] = '话题不存在';
        }else{
            $data = array(
                'uid' => $this->userinfo['id'],
                'tid' => $this->data['tid'],
                'created' => time()
            );
            $uid = $this->userinfo['id'];
            $tid = $this->data['tid'];
            $collect = $this->db->query("select * from boc_concerns where uid=$uid and tid=$tid order by id desc limit 1")->result_array();
            if(!empty($collect)){
                $this->vdata['returnCode'] = '250';
                $this->vdata['returnInfo'] = '不可重复关注';
            }else{
                $info = $this->mconcerns->create_long($data);
                if ($info) {
                    //关注话题成功生成圈子
                    $create_data = array(
                        'uid'=>$this->userinfo['id'],
                        't_id'=>$this->data['tid'],    //话题ID
                        'c_type'=>3,
                        'r_content'=>'关注了'.$article_info['title'],
                        'content'=>'关注了'.$article_info['title'],
                        'timeline'=>time()
                    );
                    $this->mcircle->create($create_data);
                    $this->mtopic->set($tid,array('hots','hots + 1'));
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['content'] = $info;
                } else {
                    $this->vdata['returnCode'] = '250';
                    $this->vdata['returnInfo'] = '操作失败';
                }
            }

        }
        $this->_send_json($this->vdata);
    }
    //修改我的关注
    public function cupdate() {
        $collect = $this->mconcerns->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$this->data['tid']));
        if($collect) {
            $tid = $collect['tid'];
            $circle_info = $this->mcircle->get_one(array('uid'=>$this->userinfo['id'],'t_id'=>$this->data['tid'],'c_type'=>3));
            $long = $this->mtopic->get_one($tid,'concern');
            if($long) {
                $this->mconcerns->del($collect['id']);
                //并且删除这条话题的圈子
                if(!empty($circle_info)){
                    $this->mcircle->del($circle_info['id']);
                }
                $this->mtopic->set($tid,array('concern','concern - 1'));
                $this->mtopic->set($tid,array('hots','hots - 1'));
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['content'] = array();
            } else {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '此长文章不存在';
            }

        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '此记录不存在';
        }
        $this->_send_json($this->vdata);
    }

}